package com.china08.yxyapi.api.home.activity.weike;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResVersion;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.repository.db.YxyResVersionRepository;
import com.china08.yxyapi.util.BookIdUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class ActivityVersionController extends YxyApiBaseController {

    @Autowired
    private YxyResVersionRepository yrvr;

    @Autowired
    private YxyResBookRepository yrbr;

    @ApiOperation(value = "获得版本")
    @RequestMapping(value = "/api_yw/home/activity/tea/version", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(@RequestBody ReqModel reqModel) {
        List<YxyResVersion> versions = new ArrayList<YxyResVersion>();
        List<RespModel> list = new ArrayList<ActivityVersionController.RespModel>();

        List<String> list2 = BookIdUtil.getBookId(reqModel.phase.trim());
        List<String> versionIds = yrbr.findVersionIdByPhaseIdAndSubjectIdGroupByVersionId1(reqModel.phase,
                reqModel.subject, list2);

        if (versionIds.size() > 0) {
            versions = yrvr.findByIdInAndStatusOrderByOrderNum(versionIds, 1);
        }
        for (YxyResVersion yxyResType : versions) {
            RespModel model = new RespModel();
            model.id = yxyResType.getId();
            model.name = yxyResType.getName();
            list.add(model);
        }
        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String phase;
        public String subject;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(phase, null), "请指定学段");
            Assert.notNull(StringUtils.defaultIfBlank(subject, null), "请指定学科");
        }

    }

    protected static class RespModel {
        public String id;
        public String name;
    }

}
